29 Lecture
CS201
Midterm & Final Term Short Notes
Friend functions
In C++, a friend function is a non-member function that is given access to the private and protected members of a class. This allows the friend function to manipulate and access the private and protected data members of the class. Friend functio
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is a friend function in C++? A) A function declared inside a class that can only access public members B) A member function that can access private and protected members of another class C) A non-member function that is given access to the private and protected members of a class D) A global function that is restricted from accessing the data members of a class
Answer: C
- Which keyword is used to declare a friend function inside a class? A) friend B) private C) public D) protected
Answer: A
- Can a friend function access the private members of a class? A) Yes B) No
Answer: A
- Which of the following is an advantage of using friend functions? A) It simplifies the implementation of certain operations B) It enhances encapsulation of class data C) It improves program maintainability D) It reduces code complexity
Answer: A
- Which of the following is a disadvantage of using friend functions? A) It reduces encapsulation and can make the program harder to maintain B) It requires the use of complex syntax to declare and define the function C) It can lead to security vulnerabilities in the program D) It can only be used for simple operations and cannot handle complex tasks
Answer: A
- Can a friend function be a member function of another class? A) Yes B) No
Answer: A
- What is the access level of a friend function in relation to the class it is declared in? A) Private B) Protected C) Public D) It is not a member of the class, so access level does not apply
Answer: D
- Can a friend function be overloaded? A) Yes B) No
Answer: A
- Can a friend function be declared in the private section of a class? A) Yes B) No
Answer: A
- Which of the following is an example of a scenario where a friend function would be useful? A) To add two numbers together B) To sort a list of objects C) To calculate the distance between two objects in a class D) To print the contents of a private data member
Answer: D
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a friend function in C++? A friend function in C++ is a function that is not a member of a class but has access to the private and protected members of the class.
What is the syntax for declaring a friend function? The syntax for declaring a friend function is as follows: friend return_type function_name(class_name &object);
What is the difference between a friend function and a member function? A friend function is not a member of the class, but has access to the private and protected members of the class. A member function is a function that is a part of the class and has access to all the members of the class.
Can a friend function be called without an object of the class? Yes, a friend function can be called without an object of the class.
Can a friend function be declared inside a class? Yes, a friend function can be declared inside a class.
Can a friend function be inherited by the derived class? No, a friend function cannot be inherited by the derived class.
What is the difference between a friend class and a friend function? A friend class is a class that has access to the private and protected members of another class. A friend function is a function that has access to the private and protected members of a class.
What is the use of a friend function? A friend function is used to access the private and protected members of a class from outside the class.
Can a friend function be overloaded? Yes, a friend function can be overloaded.
What is the scope of a friend function? The scope of a friend function is global, but it has access to the private and protected members of the class.